package com.itheima.tliasbackend;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * JwtDemo
 *
 * @author liliudong
 * @version 1.0
 * @description
 * @date 2023/5/24 13:54
 */
public class JwtDemo {

    public static void main(String[] args) {
        Map<String, Object> claims = new HashMap<>();
        claims.put("userId", 1234567);
        claims.put("name", "Tome");

        String jwt = Jwts.builder()
                // 自定义内容(载荷)
                .setClaims(claims)
                // 签名算法
                .signWith(SignatureAlgorithm.HS256, "itheima")
                // 有效期
                .setExpiration(new Date(System.currentTimeMillis() + 24 * 3600 * 1000))
                .compact();

        System.out.println(jwt);

        Claims itheima = Jwts.parser()
                .setSigningKey("itheima")
                // parseClaimsJws
                .parseClaimsJws(jwt)
                .getBody();

        System.out.println(itheima);

    }
}
